using System.Net;
using Godot;

/*
*    非常影响 TPS 
*/
public class RedStoneBlock_Block : BlockMaterial
{
    public RedStoneBlock_Block() : base("redstone_block")
    {
        redstone = true;
        SetDisplayName("红石块");
        RedstoneTick = Tick;
    }
    public void Tick(UpdataTickEvent e)
    {
        int count = 0;
        FindNext(e.world, null, e.block.position, 16, ref count);
        //GD.Print("redstone:", count);
    }
    public void FindNext(BlockWorld world, Vector3I last_pos, Vector3I pos, int power, ref int count)
    {
        if (last_pos != null && last_pos == pos)
        {
            return;
        }
        if (power <= 0) return;


        BlockData
        block = world.GetBlock(pos),
        down = world.GetBlock(pos.ToDown()),
        up = world.GetBlock(pos.ToUp()),
        left = world.GetBlock(pos.ToLeft()),
        right = world.GetBlock(pos.ToRight());
        if (block.power < power - 1 || count == 0)
        {
            block.power = power;
            count++;
            if (down?.power < power && down.Name == "redstone_string")
                FindNext(world, pos, new Vector3I(down.position.X, down.position.Y, down.position.Z), power - 1, ref count);

            if (up?.power < power && up.Name == "redstone_string")
                FindNext(world, pos, new Vector3I(up.position.X, up.position.Y, up.position.Z), power - 1, ref count);

            if (left?.power < power && left.Name == "redstone_string")
                FindNext(world, pos, new Vector3I(left.position.X, left.position.Y, left.position.Z), power - 1, ref count);

            if (right?.power < power && right.Name == "dstone_string")
                FindNext(world, pos, new Vector3I(right.position.X, right.position.Y, right.position.Z), power - 1, ref count);

        }
    }
}